package com.shepin.activity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.shepin.R;
import com.shepin.adapter.ListViewScrollAdapter;
import com.shepin.base.BaseActivity;
import com.shepin.widget.AutoListView;
import com.shepin.widget.AutoListView.OnLoadListener;
import com.shepin.widget.AutoListView.OnRefreshListener;
import com.shepin.widget.CHScrollView;
import com.shepin.widget.CHScrollView.CHScrollViewHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by apple on 16/8/4.
 */
public class TestActivity extends BaseActivity implements OnRefreshListener, OnLoadListener, OnItemClickListener{

    private AutoListView lstv;
    private CHScrollView headerScroll;
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    private ListViewScrollAdapter adapter;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg) {
            List<Map<String, String>> result = (List<Map<String, String>>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    list.clear();
                    list.addAll(result);
                    break;
                case AutoListView.LOAD:
                    lstv.onLoadComplete();
                    list.addAll(result);
                    break;
            }
            lstv.setResultSize(result.size());
            adapter.notifyDataSetChanged();
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        initView();
        initData();
    }

    private void initView() {

        headerScroll = (CHScrollView) findViewById(R.id.item_scroll_title);
        CHScrollViewHelper.mHScrollViews.clear();
        // 添加头滑动事件
        CHScrollViewHelper.mHScrollViews.add(headerScroll);
        lstv = (AutoListView) findViewById(R.id.scroll_list);

        adapter = new ListViewScrollAdapter(this, list, R.layout.auto_listview_item,
                new String[] { "title", "data_1", "data_2", "data_3", "data_4", "data_5", "data_6", },
                new int[] { R.id.item_title, R.id.item_data1, R.id.item_data2, R.id.item_data3, R.id.item_data4,
                        R.id.item_data5, R.id.item_data6 },
                R.id.item_scroll, lstv);

        lstv.setAdapter(adapter);
        lstv.setOnRefreshListener(this);
        lstv.setOnLoadListener(this);
        lstv.setOnItemClickListener(this);
    }

    private void initData() {
        loadData(AutoListView.REFRESH);
    }

    private void loadData(final int what) {
        // 这里模拟从服务器获取数据
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = handler.obtainMessage();
                msg.what = what;
                msg.obj = getData();
                handler.sendMessage(msg);
            }
        }).start();
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }

    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        try {
            TextView textView = (TextView) arg1.findViewById(R.id.item_data2);

            Toast.makeText(this, "你点击了：" + textView.getText(), Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {

        }
    }

    /**
     * class ScrollAdapter extends SimpleAdapter {
     *
     * private List<? extends Map<String, ?>> datas; private int res; private
     * String[] from; private int[] to; private Context context; private int
     * resScroll;
     *
     * public ScrollAdapter(Context context, List<? extends Map<String, ?>>
     * data, int resource, String[] from, int[] to, int resourceItem) {
     * super(context, data, resource, from, to); this.context = context;
     * this.datas = data; this.res = resource; this.from = from; this.to = to;
     * this.resScroll = resourceItem; }
     *
     * @Override public int getCount() {
     *
     *           return this.datas.size(); }
     *
     * @Override public View getView(int position, View convertView, ViewGroup
     *           parent) { View v = convertView; if (v == null) { v =
     *           LayoutInflater.from(context).inflate(res, null); //
     *           第一次初始化的时候装进来 CHScrollViewHelper.addHViews((CHScrollView)
     *           v.findViewById(resScroll), lstv); View[] views = new
     *           View[to.length]; for (int i = 0; i < to.length; i++) { View tv
     *           = v.findViewById(to[i]); views[i] = tv; } v.setTag(views); }
     *           v.setBackgroundResource(R.drawable.selector_bg_white_gray);
     *           View[] holders = (View[]) v.getTag(); int len = holders.length;
     *           for (int i = 0; i < len; i++) { ((TextView)
     *           holders[i]).setText(this.datas.get(position).get(from[i]).
     *           toString()); } return v; } }
     **/
    // 测试数据
    public List<Map<String, String>> getData() {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        Map<String, String> data = null;
        for (int i = 0; i < 10; i++) {
            data = new HashMap<String, String>();
            data.put("title", "Title_" + i);
            data.put("data_" + 1, "Date_" + 1 + "_" + i);
            data.put("data_" + 2, "Date_" + 2 + "_" + i);
            data.put("data_" + 3, "Date_" + 3 + "_" + i);
            data.put("data_" + 4, "Date_" + 4 + "_" + i);
            data.put("data_" + 5, "Date_" + 5 + "_" + i);
            data.put("data_" + 6, "Date_" + 6 + "_" + i);
            result.add(data);
        }
        return result;
    }


}
